Sort (ascending and descending) by valueΒΆ
Write a python script to sort (ascending and descending) a dictionary by value.
import operator
D = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
print('Original dictionary : ', D)
sorted_d = sorted(D.items(), key=operator.itemgetter(0))
print('Dictionary in ascending order by value : ', sorted_d)
sorted_d = sorted(D.items(), key=operator.itemgetter(0), reverse=True)
print('Dictionary in descending order by value : ', sorted_d)
Output:
Original dictionary : {0: 0, 1: 2, 2: 1, 3: 4, 4: 3}
Dictionary in ascending order by value : [(0, 0), (1, 2), (2, 1), (3, 4), (4, 3)]
Dictionary in descending order by value : [(4, 3), (3, 4), (2, 1), (1, 2), (0, 0)]